package gen;


import java.io.IOException;
import java.util.Hashtable;
import java.util.Date;
import javax.microedition.rms.*;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author lchemina
 */
public class BaseRagot {

   private Hashtable theRagots;
   RecordStore thePersistentRagots = null;



    BaseRagot() {

        theRagots = new Hashtable();
        try {
            this.thePersistentRagots = RecordStore.openRecordStore("myDBfile", true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

         }
    

    private Ragot creerNouveauRagot(String titre, String message, String auteur) {
        Ragot r=new Ragot(titre, message, auteur);
        this.theRagots.put(new Integer(message.hashCode()), r);
        return r;
        
    }

     private Ragot ajouterRagot(String titre, String message, String auteur, Date d) {
        Ragot r=new Ragot(titre, message, auteur, d);
        this.theRagots.put(new Integer(message.hashCode()), r);
        return r;

    }

    private void ajouterRagotFromBytes(byte []b) {

       ByteArrayInputStream baos = new ByteArrayInputStream(b);
       DataInputStream dos = new DataInputStream(baos);
        try {
            ajouterRagot(dos.readUTF(), dos.readUTF(), dos.readUTF(), new Date(dos.readLong()));
        } catch (IOException ex) {
            ex.printStackTrace();
        }


    }

    public Integer [] getHashNumber()
    {
        Integer []liste=new Integer[this.theRagots.size()];
        int num=0;


          java.util.Enumeration Renum = this.theRagots.keys();

         while (Renum.hasMoreElements())
         {
            liste[num++]=(Integer)Renum.nextElement();
         }

          return liste;

    }

    //cree un nouveau ragot qui va persister
    public void nouveauPersistentRagot(String titre, String message, String auteur)
    {

        Ragot r = creerNouveauRagot(titre,message,auteur);


        byte[] b = r.getBytes();
        try {
            this.thePersistentRagots.addRecord(b, 0, b.length);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

       

    }

    //charge tous les ragots en memoire
     public void loadPersistentRagot()
     {
        
     
        int num = 1;//pour les compter
        int ID=0;
        RecordEnumeration renum = null;

        try {
            renum = this.thePersistentRagots.enumerateRecords(null, null, false);
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }

        System.out.println("nombre record persistant : " + renum.numRecords());

        //on passe a travers les record
        while (renum.hasNextElement())
        {
            try {
                //on recup l'ID du record/ragot
                ID = renum.nextRecordId();
            } catch (InvalidRecordIDException ex) {
                ex.printStackTrace();
            }

                //on init un array de byte suivant la taille du record/ragot
            byte []b;

            try {
                b = new byte[this.thePersistentRagots.getRecordSize(ID)];
                this.thePersistentRagots.getRecord(ID, b, 0);
                ajouterRagotFromBytes(b);
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
          }
        }
  
    public Ragot getRagot(Integer hashRagot) {
        return (Ragot) this.theRagots.get(String.valueOf(hashRagot));
    }
    
    public int sizeRagot()
    {
        return this.theRagots.size();
    }

    public java.util.Enumeration getEnumerationRagots()
    {
        return this.theRagots.elements();
    }

    public Hashtable getHashtable()
    {
        return this.theRagots;
    }
}
